<?php
/**
 * （logic）海外仓运单异常
 */
namespace App\Logic\OverSeasIn;

use App\Models\OverSeasIn\Delivery;
use App\Models\OverSeasIn\DeliveryLogistics;
use Illuminate\Support\Facades\DB;
class TrackExCheck
{
    /**
     * 海外仓-中转
     */
    public static function handle()
    {
        //发货后超过一周未录入运单号记为异常
        $date = date('Y-m-d 00:00:00',strtotime('-7 days'));

        //海外仓-中转发货
        Delivery::query()
            ->where('delivery_type', Delivery::OS_DELIVERY_TRANSFER) //海外仓-中转
            ->where('track_ex_status',0)
            ->where('delivered_at','<',$date)
            ->orderBy('id')
            ->chunk(100, function ($items){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        $dl = DeliveryLogistics::query()
                            ->where('delivery_no',$item->delivery_no)
                            ->first();

                        //运单号异常状态 （0：待录入 1：未录入(异常) 2：已录入(正常)）
                        if($dl){
                            if($dl->tracking_no){
                                $item->track_ex_status = 2;
                            }else{
                                $item->track_ex_status = 1;
                            }
                        }else{
                            $item->track_ex_status = 1;
                        }
                        $item->save();
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
        });
    }

    /**
     * 海外仓-代发
     */
    public static function handle1()
    {
        //发货后超过一周未录入运单号记为异常
        $date = date('Y-m-d 00:00:00',strtotime('-7 days'));

        //海外仓-中转发货
        Delivery::query()
            ->where('delivery_type', Delivery::OS_DELIVERY_DROPSHIPPING) //海外仓-代发
            ->where('track_ex_status',0)
            ->where('delivered_at','<',$date)
            ->orderBy('id')
            ->chunk(100, function ($items){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        $dl = DeliveryLogistics::query()
                            ->where('delivery_no',$item->delivery_no)
                            ->first();

                        //运单号异常状态 （0：待录入 1：未录入(异常) 2：已录入(正常)）
                        if($dl){
                            if($dl->tracking_no){
                                $item->track_ex_status = 2;
                            }else{
                                $item->track_ex_status = 1;
                            }
                        }else{
                            $item->track_ex_status = 1;
                        }
                        $item->save();
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });
    }

}
